﻿var paginationTotal = 20;//记录总数
var isSearch = false;
function submitsearch() {
    isSearch = true;
    paginationCurrentPage = 1;
    initData(0);
}

function loadData(page, pageSize) {
    var vForm = "";
    if (isSearch == true && isSearch != undefined) {
        vForm = $("#sForm").serializeArray();
    }
    $.ajax({
        type:"POST",
        async:false,
        dataType:"json",
        url:"/warehouse/area/list/page/" + page + "/pageSize/" + pageSize,
        data:vForm,
        success:function (json) {
            var list = "";
            var module = '';
            paginationTotal = json.count;
            if (json.ask == 0) {
                list = "<td colspan='8' class=\"center\">&nbsp;<{$lang.no_data}></td>";
            } else {
                var i = page == 1 ? 1 : pageSize * (page - 1) + 1;
                $.each(json.result, function (key, val) {
                    var statusIcon = val.wa_status == 0 ? "unnormal.png" : "normal.png";
					var titleStr =  val.wa_status == 0 ? "<{$lang.unuseable}>" : "<{$lang.useable}>";
                    if ((key + 1) % 2 == 1) {
                        list += "<tr class=''>";
                    } else {
                        list += "<tr class='even-tr'>";
                    }
                    var poc_value = val.poc_value == null ? "" : val.poc_value;
					list += "<td>" + (i++) + "</td>";
                    list += "<td>" + val.wa_name + "</td>";
                    list += "<td>" + val.wa_code + "</td>";
                    list += "<td>" + poc_value + "</td>";
                    list += "<td>" + val.wa_type + "</td>";
                    list += "<td>" + val.wa_create_time + "</td>";
                    list += "<td>" + val.wa_last_update + "</td>";
                    list += "<td class=\"center\"><img title="+titleStr+" src=\"/images/icon/" + statusIcon + "\" ></td>";
                    list += "<td class=\"center\"><a href=\"javascript:edit(" + val.wa_id + ")\"><img src=\"/images/icon/edit.gif\" title=\"<{$lang.edit}>\"></a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href=\"javascript:setshelf(" + "\'" + val.wa_code + "\'" + "," + "\'" + val.wa_name + "\'" + ")\" ><img title=\"<{$lang.detail}>\" src=\"/images/icon/info.jpg\"></a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href=\"javascript:delelteInfo(" + val.wa_id + ")\"><img title=\"<{$lang.delete}>\" src=\"/images/icon/delete.gif\"></a></td>";
                    list += "</tr>";
                });
            }
            $("#loadListData").html(list);
        }
    });
}

//<{$lang.view}><{$lang.ws_code}>
function setshelf(wa_code, wa_name) {
    if (wa_code == '') {
        return false;
    }
    var url = '/warehouse/area/getlist/wa_code/' + wa_code + '/wa_name/' + wa_name;
    window.parent.$('<iframe id="ifr_shelf_list" name="ifr_shelf_list" src="' + url + '" />').dialog({
        title:'<{$lang.view}><{$lang.ws_code}>',
        autoOpen:true,
        width:760,
        modal:false,
        resizable:false,
        position:'top',
        autoResize:true,
        close:function () {
            $(this).remove();
        },
        buttons:{
            'Close/<{$lang.close}>':function () {
                $(this).remove();
            }
        }
    }).width(730).height(450);
}

//<{$lang.delete}>
function delelteInfo(id) {
    if (id == '' || id == undefined) {
        return false;
    }
    Uid = id;
    $('#right_confirm').dialog('open');
}

function dialogclose() {
    $("#dialog").dialog("close");
}

$(function () {
//    initData(0);
    loadSelect();//<{$lang.warehouse}>
    $("#dialog").dialog({
        autoOpen:false,
        modal:true,
        width:380,
        show:"slide",
        buttons:{
            '<{$lang.ok}>':function () {
                create();
            },
            '<{$lang.cancel}>':function () {
                setpoc(this, 0);
                $("#createForm")[0].reset();
                $(this).dialog('close');
            }
        }
    });

    $("#right_confirm").dialog({
        autoOpen:false,
        modal:true,
        height:160,
        show:"slide",
        buttons:{
            '<{$lang.ok}>':function () {
                $.ajax({
                    type:"post",
                    async:false,
                    dataType:"json",
                    url:"/warehouse/area/delete",
                    data:"wa_id=" + Uid,
                    success:function (json) {
                        if (json.ask == '1') {
                            paginationTotal = paginationTotal - 1;
                            initData(paginationCurrentPage - 1);
                            messags = "<{$lang.success}><{$lang.delete}>！";
                        } else {
                            messags = "<{$lang.delete}><{$lang.fail}>！";
                        }
                        alertTip(messags);
                    }
                });
                $(this).dialog('close');
            },
            '<{$lang.cancel}>':function () {
                $(this).dialog('close');
            }
        }

    });

    $("#create").click(function () {
        tip('');
        $("#div_wa_code").html('<input type="text" name="wa_code" id="wa_code" class="text ui-widget-content"/>');
        $("#dialog").dialog({title:'<{$lang.add}><{$lang.area}>', autoOpen:true});
        tip('');//<{$lang.clear}><{$lang.tip}><{$lang.msg}>
        $("#createForm")[0].reset();
        $("#createForm input").each(function () {
            $(this).val('');
        });
    });
    $("#close").click(function () {
        dialogclose();
    });
});

function create() {
    vForm = "";
    vForm = $("#createForm").serializeArray();
    $.ajax({
        type:"post",
        async:false,
        dataType:"json",
        url:"/warehouse/area/create",
        data:vForm,
        success:function (json) {
            if (json.ask != '0') {
                loadData(paginationCurrentPage, paginationPageSize);
                if (json.ask == 1) {
                    alertTip('<{$lang.add}><{$lang.success}>');
                    dialogclose();
                } else if (json.ask == 2) {
                    alertTip('<{$lang.edit}><{$lang.success}>');
                    dialogclose();
                } else {
                    tip("<{$lang.edit}><{$lang.data}>?");
                }
            } else {
                if (json.error == null)return;
                var html = '';
                $.each(json.error, function (key, val) {
                    html += '<span><img src="/images/no.gif">' + val + '</span>';
                })
                tip(html);
            }
        }
    });
}
//<{$lang.warehouse}><{$lang.list}>
function loadSelect() {
    $.ajax({
        type:"post",
        async:false,
        dataType:"json",
        url:"/warehouse/house/select",
        data:null,
        success:function (json) {
            var list = "";
            if (json.ask == 1) {
                $.each(json.result, function (key, val) {
                    list += "<option value=" + val.warehouse_id + ">" + val.warehouse_name + "</option>";
                });
            }
            $("#warehouse_id").html(list);
        }
    });
}

function tip(mess) {
    $("#tips").html(mess);
    if (mess != '') {
        $("#tips").show();
    } else {
        $("#tips").hide();
    }
}

function edit(id) {
    tip('');//<{$lang.clear}><{$lang.tip}><{$lang.msg}>
    $.ajax({
        type:"post",
        async:false,
        dataType:"json",
        url:"/warehouse/area/info",
        data:"wa_id=" + id,
        success:function (json) {
            if (json.ask == 1) {
                if (json.result.poc_id != 0) {
                    setpoc(this, 1);
                }
                $("[name=wa_name]").val(json.result.wa_name);
                $("#div_wa_code").html(json.result.wa_code + "<input type=\"hidden\" value=\"" + json.result.wa_code + "\" name=\"wa_code\" id=\"wa_code\" />");
                $("[name=wa_status]").val(json.result.wa_status);
                $("[name=warehouse_id]").val(json.result.warehouse_id);
                $("[name=wa_id]").val(json.result.wa_id);
                $("[name=poc_id]").val(json.result.poc_id);
                $("[name=wa_type]").val(json.result.wa_type);
                $("#dialog").dialog({title:'<{$lang.edit}><{$lang.area}>', autoOpen:true});
            }
        }
    });
}
//<{$lang.operation}><{$lang.tip}>
function alertTip(tip) {
    $('<div title="<{$lang.tip}>"><p align="center">' + tip + '</p></div>').dialog({
        modal:true,
        buttons:{
            '<{$lang.ok}>':function () {
                $(this).dialog("close");
            }
        }
    });
}

function setpoc(Obj, val) {
    if (Obj.checked == true || val == 1) {
        $("#OptionChoose").show();
        $("#check_poc").attr("checked", true).val(1);
    } else {
        $("#OptionChoose").hide();
        $("#check_poc").attr("checked", false).val(0);
    }
}